package com.dzienniczekwedkarza.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.dzienniczekwedkarza.R;


/**
 * @author Rafał Wysogląd
 * 
 */

public class ContactFragment extends SherlockFragment {
	
	
	private EditText Email;
	private EditText Subject;
	private EditText AddressFrom;
	private Button   WyslijWiadomosc;
	
	

	    @Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	        
		View view = inflater.inflate(R.layout.fragment_contact, container, false);
		
		Email = (EditText) view.findViewById(R.id.editTextEmail);
		Subject = (EditText) view.findViewById(R.id.editTextSubject);
		AddressFrom = (EditText) view.findViewById(R.id.editTextAddressFrom);
		WyslijWiadomosc = (Button) view.findViewById(R.id.buttonWyslijWiadomosc);
		
		
		WyslijWiadomosc.setOnClickListener(new View.OnClickListener() {
		
		  public void onClick(View v) {
			  
			String to      = "rafal.wysoglad@gmail.com";
			String from    = AddressFrom.getText().toString();
	        String subject = Subject.getText().toString();
	        String message = Email.getText().toString();  
				 

	           //If email ID is not entered, display toast message
	  			if (from != null && from.length() == 0) {
	  			      Toast.makeText(getSherlockActivity(), "Zapomniałeś podać swój adres email" ,Toast.LENGTH_SHORT).show() ;
	  			}
	  			//If entered email ID is not valid, display toast message
		        else if (from != null && from.length() > 0 && !isEmailValid(from)) {
		              Toast.makeText(getSherlockActivity(), "Wpisany adres email nie jest poprawny", Toast.LENGTH_SHORT).show();
		        }
	  			//If subject is empty, display toast message
	  		    else if (subject != null && subject.length() == 0) {
	  				  Toast.makeText(getSherlockActivity(), "Zapomniałeś podać temat wiadomości", Toast.LENGTH_SHORT).show();
	  		    }
	  		    //If message is empty, display toast message
		        else if (message != null && message.length() == 0) { 
		        	Toast.makeText(getSherlockActivity(), "Zapomniałeś napisać treść wiadomości", Toast.LENGTH_SHORT).show();
		        }      
	        	
	            //If all three controls are entered with valid values
	  			else if (from != null && subject != null && message != null) {
				
	        	Intent email = new Intent(Intent.ACTION_SEND);
	        	
	        	email.putExtra(Intent.EXTRA_EMAIL, new String[] { to } );
	        	email.putExtra(Intent.EXTRA_TEXT, from );
				email.putExtra(Intent.EXTRA_SUBJECT, subject);
				email.putExtra(Intent.EXTRA_TEXT, message);
				email.setType("message/rfc822");
				startActivity(Intent.createChooser(email, "Wybierz klienta pocztowego:"));
   
   			};	
   			
	  }
		  
		    //Boolean method to check if entered email ID is valid or not
			boolean isEmailValid(CharSequence email) {
			return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
			}
		
});
		
		
		
         return view;
	  }
}

